package com.forbet.demo.util;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.ConnectionConfigurations;
import com.alibaba.dashscope.tokenizers.Tokenization;
import com.alibaba.dashscope.tokenizers.TokenizationResult;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author:hading
 * @create: 2024-07-16 11:35
 * @Description: 通义千问 工具类
 */
public class AlibabaUtils {

    //    qwen-plus 支持30k token的上下文
    public static GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .model("qwen-plus-0806").apiKey("sk-5eda12972783498b831f23469334f1f2")
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.5)
                .enableSearch(true)
                .build();
    }



    public static GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }

//    public static void main(String[] args) {
//
//    }

    public static Message createMessage(Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }

    private static void printResult(GenerationResult result) {
        System.out.println(result);
    }

    private static void printResultAsJson(GenerationResult result) {
        System.out.println(JsonUtils.toJson(result));
    }

    public static void ms(){
//        Constants.connectionConfigurations = ConnectionConfigurations.builder()
//                .connectTimeout(Duration.ofSeconds(500))  // set connection timeout, default 120s
//                .readTimeout(Duration.ofSeconds(500)) // set read timeout, default 300s
//                .writeTimeout(Duration.ofSeconds(500)) // set read timeout, default 60s
//                .connectionIdleTimeout(Duration.ofSeconds(500)) // connection pool idle timeout, default 300s
//                .connectionPoolSize(32) // idle connections in the okhttp connection pool.
//                .maximumAsyncRequests(32)  // async requests limit.
//                .maximumAsyncRequestsPerHost(32) // async request host limit.
//                .proxyHost("The http proxy host") // set proxy host, if set will use proxy. default null.
//                .proxyPort(443) // set proxy port, default 443
//                .proxyAuthenticator(null) // you can customize you proxy authenticator. default null.
//                .build();
    }


//计算token的官方工具

    //计算token
//    Tokenization tokenizer = new Tokenization();
//    Message userMsg = Message.builder()
//            .role(Role.USER.getValue())
//            .content(bigStr + asianStr + europeStr)
//            .build();
//    GenerationParam param2 = GenerationParam.builder()
//            .model(Tokenization.Models.QWEN_TURBO)
//            .messages(Collections.singletonList(userMsg))
//            .apiKey("sk-5eda12972783498b831f23469334f1f2") //API-KEY
//            .build();
//    TokenizationResult result2 = tokenizer.call(param2);
//    System.out.println(JsonUtils.toJson(result2));
}
